package com.trailwalker.traceability.activity;

import com.trailwalker.javafx.core.activity.Activity;
import com.trailwalker.javafx.core.entity.Intent;
import com.trailwalker.javafx.utils.storage.PreferencesManager;
import com.trailwalker.javafx.utils.style.AdaptUtils;
import com.trailwalker.javafx.utils.style.StyleUtils;
import com.trailwalker.traceability.entitiy.constant.ProcessConstant;
import com.trailwalker.traceability.entitiy.enum_entity.ProcessType;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

/**
 * @author Trailwalker
 * @create 2021-11-11 21:00
 */
public class MainActivity extends BaseActivity {

    private AnchorPane mApContent;

    @Override
    protected void afterSuperCreate() {
        initLoad();
        initView();
        initData();
        initListener();
        onReady();
    }

    private void onReady() {

    }

    private void initListener() {

    }

    private void initData() {

    }

    private void initView() {
        VBox vbContent = new VBox();
        mApContent.getChildren().add(vbContent);
        mAdaptUtils.setMatchParentAnchorPane(vbContent);
        vbContent.setAlignment(Pos.CENTER);
        vbContent.setSpacing(mAdaptUtils.getScale(50));

        GridPane gpContainer = new GridPane();
        vbContent.getChildren().add(gpContainer);
        gpContainer.setAlignment(Pos.CENTER);
        gpContainer.setHgap(mAdaptUtils.getScale(50));
        gpContainer.setVgap(mAdaptUtils.getScale(50));
        ColumnConstraints column1 = new ColumnConstraints();
        column1.setHalignment(HPos.RIGHT);
        gpContainer.getColumnConstraints().add(column1);
        ColumnConstraints column2 = new ColumnConstraints();
        column2.setHalignment(HPos.LEFT);
        gpContainer.getColumnConstraints().add(column2);

        for (int i = 0; i < ProcessConstant.processTypeList.size() / 3; i++) {
            for (int j = 0; j < 3; j++) {
                ProcessType processType = ProcessConstant.processTypeList.get(i * 3 + j);
                Button button = new Button();
                gpContainer.add(button, j, i);
                button.setText(processType.getName());
                mAdaptUtils.setScaleSize(button, 200, 100);
                mAdaptUtils.setScaleTextSize(button, 25);
                StyleUtils.setBackgroundColor(button, "#026cba");
                StyleUtils.setTextColor(button, "#ffffff");
                StyleUtils.setBorderRadius(button, mAdaptUtils.getScale(200), mAdaptUtils.getScale(100), mAdaptUtils.getScale(30));
                button.setCursor(Cursor.HAND);
                button.setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        if (event.getButton().equals(MouseButton.PRIMARY)) {
                            Class<? extends Activity> activityClass = mApplication.getActivityClass(processType);
                            if (activityClass != null) {
                                PreferencesManager.getInstance().putInt("processTypeIndex", processType.getIndex());
                                Intent intent = new Intent(activityClass);
                                startActivity(intent);
                            }
                        }
                    }
                });
            }
        }

    }

    private void initLoad() {

    }

    @Override
    protected Node initContentView() {
        mApContent = new AnchorPane();
        return mApContent;
    }
}
